/*
 * @Author: Lin Hongwei
 * @Date: 2022-10-08 19:48:22
 * @LastEditTime: 2022-11-05 15:07:56
 * @LastEditors: Lin Hongwei
 * @email: 1365755396@qq.com
 * @CSDN: https://blog.csdn.net/weixin_45429409
 * @Description: 看板请求接口
 */
import { QueryKey, useMutation, useQuery } from "react-query";
import { useHttp } from ".";
import { Kanban } from "../types/kanban";
import { SortParam } from "../types/sort-param";
import { cleanObject } from "../utils";
import {
  useOptimisticAddConfig,
  useOptimisticDeleteConfig,
  useOptimisticSortConfig,
  useOptimisticUpdateConfig,
} from "../utils/use-optimistic-options";

/**
 * @description: 看板列表查询
 * @param {QueryKey} queryKey - 查询请求缓存的值
 * @param {Partial} param - 请求的参数
 * @return {*}
 */
export const useQueryKanban = (queryKey: QueryKey, param?: Partial<Kanban>) => {
  const client = useHttp();

  return useQuery<Kanban[]>(queryKey, () =>
    client(`/kanban/${param?.projectId}`)
  );
};

/**
 * @description: 添加看板
 * @param {QueryKey} queryKey
 * @return {*}
 */
export const useAddKanban = (queryKey: QueryKey) => {
  const client = useHttp();
  return useMutation(
    (data: Partial<Kanban>) =>
      client("/kanban/create", {
        method: "POST",
        data,
      }),
    useOptimisticAddConfig(queryKey)
  );
};

/**
 * @description: 删除看板
 * @param {QueryKey} queryKey
 * @return {*}
 */
export const useDeleteKanban = (queryKey: QueryKey) => {
  const client = useHttp();
  return useMutation(
    (data: Partial<Kanban>) =>
      client(`/kanban/del`, {
        method: "POST",
        data: { id: data.id },
      }),
    useOptimisticDeleteConfig(queryKey)
  );
};

/**
 * @description: 更新
 * @param {QueryKey} queryKey
 * @return {*}
 */
export const useUpdateKanban = (queryKey: QueryKey) => {
  const client = useHttp();
  return useMutation(
    (data: Partial<Kanban>) =>
      client(`/kanban/edit`, {
        method: "POST",
        data,
      }),
    useOptimisticUpdateConfig(queryKey)
  );
};

export const useSortKanban = (queryKey: QueryKey) => {
  const client = useHttp();
  return useMutation(
    (data: SortParam) =>
      client(`/kanban/sort`, {
        method: "POST",
        data: {
          fromId: data.fromId,
          toId: data.toId,
        },
      }),
    useOptimisticSortConfig(queryKey)
  );
};
